package com.hospital.doctor.doctory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.doctor.doctory.dao.DoctorDiagnoseMapper;
import com.hospital.doctor.doctory.dao.PatientDiagnosisResultsMapper;
import com.hospital.doctor.doctory.entity.DoctorDiagnose;
import com.hospital.doctor.doctory.entity.PatientDiagnosisResults;
import com.hospital.doctor.doctory.service.DoctorMedicineService;
import com.hospital.frontdesk.fronxiang.dao.DoctorQueueMapper;
import com.hospital.frontdesk.fronxiang.entity.DoctorQueue;
import com.hospital.user.registration.entity.Doctor_queue;
import com.hospital.xiaochengxiu.yan.dao.DoctorMedicineMapper;
import com.hospital.xiaochengxiu.yan.entity.DoctorMedicine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author ：ysb
 * @ClassName ：DoctorMedicineServiceImpl
 * @Package ：com.hospital.doctor.doctory.service.impl
 * @Description:
 * @Date:2021/11/8 10:51
 */
@Service
public class DoctorMedicineServiceImpl implements DoctorMedicineService {

    @Autowired
    private DoctorQueueMapper doctorQueueMapper;
    @Autowired
    private DoctorMedicineMapper doctorMedicineMapper;
    @Autowired
    private PatientDiagnosisResultsMapper patientDiagnosisResultsMapper;
    @Autowired
    private DoctorDiagnoseMapper doctorDiagnoseMapper;

    /**
     * 开药
     * @param list
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public Boolean prescribe(Doctor_queue doctorQueue, List<DoctorMedicine> list, PatientDiagnosisResults patientDiagnosisResults, DoctorDiagnose doctorDiagnose) {

        //修改排队表的状态
        DoctorQueue doctorQueue1=new DoctorQueue();
        doctorQueue1.setState(2);
        QueryWrapper<DoctorQueue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("patientId",doctorQueue.getPatientId()).eq("time",doctorQueue.getTime());
        doctorQueueMapper.update(doctorQueue1,queryWrapper);
        //插入记录
        list.forEach(med->{
            doctorMedicineMapper.insert(med);
        });
        //插入诊断记录表2
        doctorDiagnoseMapper.insert(doctorDiagnose);
        //插入诊断记录表
        patientDiagnosisResultsMapper.insert(patientDiagnosisResults);
        return true;
    }
}
